package mwa.cs.li.ctrl;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mwa.cs.dao.SMApplicationLogDAO;
import mwa.cs.database.Database;
import mwa.cs.model.SMApplicationLog;
import mwa.cs.util.JDate;

public class LogoutSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public LogoutSrvl() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
    
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		Database db = new Database();
		HttpSession voSession = request.getSession();
		
		SMApplicationLog voLog = new SMApplicationLog(0, "Logout", JDate.getDate(), JDate.getTime(), 
				Integer.parseInt(voSession.getAttribute("vaUserId").toString()), "Logout", "Username : "+  voSession.getAttribute("vaUserName").toString().toString()+" ออกจากระบบ", 
				voSession.getAttribute("vaURLAddress").toString().toString(), voSession.getAttribute("vaAgentBrowser").toString().toString());
		
		SMApplicationLogDAO voSMApplicationLog = new SMApplicationLogDAO(db);
		voSMApplicationLog.addLog(voLog);
		
		response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
		response.setDateHeader("Expires", 0);
		voSession.invalidate(); 

		response.sendRedirect("index.jsp");

	}

}